package sft.Autenticacao.perfil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sft.Autenticacao.perfil.logica.PerfilAcesso;
import sft.BancodeDados.CriaConexao;

public class PerfilDao {

    private Connection conexao;

    public PerfilDao() {
        try {
            this.conexao = new CriaConexao().getConnection();
        } catch (SQLException ex) {
            System.out.println("Erro Perfil DAO");
            Logger.getLogger(PerfilDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Operacao(PerfilAcesso p) {
        
        try {
            try (PreparedStatement stmt = conexao.prepareCall(p.getSql())) {

                setSTMT(stmt, p);
                stmt.execute();
                stmt.close();
                conexao.close();
            }

        } catch (SQLException ex) {
            if(ex instanceof com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException){  
                JOptionPane.showMessageDialog(null, "Chave Estrangeira em uso, imposspível deletar! ","ERRO",JOptionPane.ERROR_MESSAGE);
            }
            System.out.println("Erro ao " + p.getOperacao());
            Logger.getLogger(PerfilDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setPrincipal(PreparedStatement stmt, PerfilAcesso p) throws SQLException {
        
        stmt.setString(1, p.getNome());
        stmt.setBoolean(2, p.isCad_aten());
        stmt.setBoolean(3, p.isCad_unid());
        stmt.setBoolean(4, p.isCad_conv());
        stmt.setBoolean(5, p.isCad_ncon());
        stmt.setBoolean(6, p.isCad_usua());
        stmt.setBoolean(7, p.isCad_perfil());
        stmt.setBoolean(8, p.isProd_inco());
        stmt.setBoolean(9, p.isProd_glos());
        stmt.setBoolean(10, p.isProd_part());
        stmt.setBoolean(11, p.isProd_cort());
        stmt.setBoolean(12, p.isProd_desc());
        stmt.setBoolean(13, p.isCad_conv_depara());
        
    }

    private void setSTMT(PreparedStatement stmt, PerfilAcesso p) throws SQLException {
        switch (p.getOperacao()) {
            case "adicionar":
                setPrincipal(stmt, p);
                break;
                
            case "editar":
                setPrincipal(stmt, p);
                stmt.setInt(14, p.getId());
                break;
                
            case "deletar":
                stmt.setInt(1, p.getId());
                break;
        }
    }
    
    public List<PerfilAcesso> getList(PerfilAcesso p1, boolean nome){
        try {
            PreparedStatement stmt = conexao.prepareStatement(p1.getSql());
            if(nome){
                stmt.setInt(1, p1.getId());
            }
            ResultSet rs = stmt.executeQuery();
            
            List<PerfilAcesso> list = new ArrayList<>();
            while(rs.next()){
                PerfilAcesso p = new PerfilAcesso();
                p.setId(rs.getInt(1));
                p.setNome(rs.getString(2));
                p.setCad_aten(rs.getBoolean(3));
                p.setCad_unid(rs.getBoolean(4));
                p.setCad_conv(rs.getBoolean(5));
                p.setCad_ncon(rs.getBoolean(6));
                p.setCad_usua(rs.getBoolean(7));
                p.setCad_perfil(rs.getBoolean(8));
                p.setProd_inco(rs.getBoolean(9));
                p.setProd_glos(rs.getBoolean(10));
                p.setProd_part(rs.getBoolean(11));
                p.setProd_cort(rs.getBoolean(12));
                p.setProd_desc(rs.getBoolean(13));
                p.setCad_conv_depara(rs.getBoolean(14));
                list.add(p);
            }
            return list;
        } catch (SQLException ex) {
            System.out.println("ERRO NA PERSQUISA");
            Logger.getLogger(PerfilDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
        
    }
}
